const MongoClient = require('mongodb').MongoClient;
const ObjectId = require('mongodb').ObjectId;
const url = "mongodb://taosha_user:apyl8XWP6LSW4JHs@101.35.6.34:27722/taosha";

async function main() {
  const client = await MongoClient.connect(url, {useNewUrlParser: true, useUnifiedTopology: true});
  const db = client.db('taosha');

  let tags = await db.collection('original_tags')
    .find()
    .sort({create_at: -1})
    .toArray();

  for (let i = 0; i < tags.length; i++) {
    let tag = tags[i];
    for (let topic_id of tag.topic_ids) {
      console.log(topic_id);
      let result = await db.collection('topics').updateOne({_id: ObjectId(topic_id)}, {'$addToSet': {tags: tag.name}});
      console.log(result.result);
    }
    console.log('------------');
  }


  client.close();
}

main();
